﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microformats.Enums;
using System.Drawing.Design;
using System.ComponentModel.Design;

namespace Microformats.Controls
{
    [PersistChildren(false)]
    [ParseChildren(true)]
    [ToolboxData("<{0}:XFNLink runat=server></{0}:XFNLink>")]
    public class XFNLink : HyperLink
    {
        public XFNLink()
        {
            this.XFN = new XFN();
        }

        [Browsable(true)]
        [Category("Behavior")]
        [Description("XFN params")]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public XFN XFN
        {
            get;
            set;
        }

        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            List<string> values = new List<string>();  

            if (XFN.Identity)
            {
                values.Add("me");
            }

            if (XFN.FriendShip != FriendShip.None)
            {
                switch (XFN.FriendShip)
                {
                    case FriendShip.Acquaintance:
                        values.Add("acquaintance");
                        break;
                    case FriendShip.Contact:
                        values.Add("contact");
                        break;
                    case FriendShip.Friend:
                        values.Add("friend");
                        break;
                    default:
                        values.Add("none");
                        break;
                }
            }

            if (XFN.Physical)
            {
                values.Add("met");
            }

            if (XFN.ProfessionalCollection != null && XFN.ProfessionalCollection.Count() > 0)
            {
                foreach (ProfessionalCollection professional in XFN.ProfessionalCollection)
                {
                    switch (professional.Professional)
                    {
                        case Professional.Colleague:
                            values.Add("colleague");
                            break;
                        case Professional.CoWorker:
                            values.Add("co-worker");
                            break;
                        default:
                            break;
                    }
                }
            }

            if (XFN.Geographical != Geographical.None)
            {
                switch (XFN.Geographical)
                {
                    case Geographical.CoResident:
                        values.Add("co-resident");
                        break;
                    case Geographical.Neighbor:
                        values.Add("neighbor");
                        break;
                    default:
                        values.Add("none");
                        break;
                }
            }

            if (XFN.Family != Family.None)
            {
                switch (XFN.Family)
                {
                    case Family.Child:
                        values.Add("child");
                        break;
                    case Family.Parent:
                        values.Add("parent");
                        break;
                    case Family.Sibling:
                        values.Add("sibling");
                        break;
                    case Family.Spouse:
                        values.Add("spouse");
                        break;
                    case Family.Kin:
                        values.Add("kin");
                        break;
                    default:
                        values.Add("none");
                        break;
                }
            }

            if (XFN.RomanticCollection != null && XFN.RomanticCollection.Count() > 0)
            {
                foreach (RomanticCollection romantic in XFN.RomanticCollection)
                {
                    switch (romantic.Romantic)
                    {
                        case Romantic.Muse:
                            values.Add("muse");
                            break;
                        case Romantic.Crush:
                            values.Add("crush");
                            break;
                        case Romantic.Date:
                            values.Add("date");
                            break;
                        case Romantic.Sweetheart:
                            values.Add("sweetheart");
                            break;
                        default:
                            break;
                    }
                }
            }

            StringBuilder rel = new StringBuilder();

            foreach (string value in values)
            {
                rel.AppendFormat("{0} ", value);
            }

            writer.AddAttribute(HtmlTextWriterAttribute.Rel, rel.ToString().Trim());

            base.AddAttributesToRender(writer);
        }

        protected override void RenderContents(HtmlTextWriter writer)
        {
            base.RenderContents(writer);
        }
    }
}
